package de.zainodis.kitchengnome.ui.adapter;

import java.util.Collections;
import java.util.List;

import android.widget.BaseAdapter;

/**
 * Adapter for lists whose items implement {@link Comparable}.
 * 
 * @author fzarrai
 * 
 */
public abstract class SortableListAdapter extends BaseAdapter {

	/**
	 * Sorts the given list, using the item's {@link Comparable} implementation
	 * if available, provided that the given list is not null and has more than
	 * zero elements.
	 * 
	 * @param toSort
	 *           the list to sort.
	 * @return true if the list was sorted; false otherwise. If the list was
	 *         sorted, it automatically makes a call to
	 *         {@link BaseAdapter#notifyDataSetChanged()}.
	 */
	protected boolean sort(List toSort) {
		if (toSort != null && toSort.size() > 0) {
			Collections.sort(toSort);
			notifyDataSetChanged();
			return true;
		}
		return false;
	}

}
